/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yurendao.OSFileSystem.view;

import com.yurendao.OSFileSystem.MyFAT.MyFAT;
import com.yurendao.OSFileSystem.data.MyFIT;
import com.yurendao.OSFileSystem.file.MyFile;
import com.yurendao.OSFileSystem.myTree.MyFileSystemTree;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.Date;
import javax.swing.JOptionPane;
import javax.swing.JTree;

/**
 *
 * @author Administrator
 */
public class NotePad extends javax.swing.JFrame {
   
     /** Creates new form NotePad */
    MyFile file;
    MyFileSystemTree mytree;
    MyFAT myfat;
    MyFIT myfit;
    JTree jTreeFileSystem;
    boolean modefied = false;
    public static String modififyTime;
    
    public NotePad(String filename,MyFile file,MyFileSystemTree mytree0,MyFAT myfat0,MyFIT myfit0,JTree jTreeFileSystem0) {
        
        initComponents();
        this.setTitle(filename);
        this.editor.setText(file.getMyfileContent());
        this.file = file;
        mytree = mytree0;
        myfat = myfat0;
        myfit = myfit0;
        jTreeFileSystem = jTreeFileSystem0;
        this.actionToWindows();
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        editor = new javax.swing.JTextArea();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItemSave = new javax.swing.JMenuItem();
        jSeparator2 = new javax.swing.JPopupMenu.Separator();
        jMenuItemExit = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        jMeauItemShow = new javax.swing.JMenuItem();
        
        
        jMenu2 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        editor.setColumns(20);
        editor.setFont(new java.awt.Font("宋体", 1, 14));
        editor.setRows(5);
        jScrollPane1.setViewportView(editor);

        jMenu1.setText("文件");

        jMenuItemSave.setFont(new java.awt.Font("楷体", 0, 14)); // NOI18N
        jMenuItemSave.setText("保存");
        jMenuItemSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemSaveActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItemSave);
        jMenu1.add(jSeparator2);

        jMenuItemExit.setFont(new java.awt.Font("楷体", 0, 14)); // NOI18N
        jMenuItemExit.setText("退出");
        jMenuItemExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemExitActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItemExit);
        jMenu1.add(jSeparator1);
        jMenuBar1.add(jMenu1);

        jMenu2.setText("编辑");
        jMeauItemShow.setText("显示");
        jMeauItemShow.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemShowActionPerformed(evt);
            }
        });
        jMenu2.add(jMeauItemShow);
        jMenuBar1.add(jMenu2);
        setJMenuBar(jMenuBar1);

        
        
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 593, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 371, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>                        

    private void jMenuItemSaveActionPerformed(java.awt.event.ActionEvent evt) {                                              
        // TODO add your handling code here:
        this.saveIt();
    }                                             

    private void jMenuItemExitActionPerformed(java.awt.event.ActionEvent evt) {                                              
        // TODO add your handling code here:
        this.closeIt();
    }                                             
    private void jMenuItemShowActionPerformed(java.awt.event.ActionEvent evt){
        this.showIt();
    }

    private void actionToWindows(){
        this.addWindowListener(new WindowListener(){
            public void windowOpened(WindowEvent e) {
            }
            @Override
            public void windowClosing(WindowEvent e) {
                closeIt();
            }
            public void windowClosed(WindowEvent e) {
            }
            public void windowIconified(WindowEvent e) {
            }
            public void windowDeiconified(WindowEvent e) {
            }
            public void windowActivated(WindowEvent e) {
            }
            public void windowDeactivated(WindowEvent e) {
            }
        });
    }

    private void saveIt(){
        if (editor.getText().equals(file.getMyfileContent())) {
            modefied = false;
        } else {
            modefied = true;
            file.setMyfileContent(editor.getText());
            //System.out.println(":::" + file.getMyfileContent());
            int length = (file).getMyfileContent().length();
            if (length % 64 > 0) {
                length = length / 64 + 1;
            } else {
                length = length / 64;
            }
            myfat.deletedFile(file.getMyFATNumber());
            myfat.createdFile(file.getMyfatherFATNumber(), length,
                    file.getMyfileContent(), file.getMyfileAttributes());
            this.mytree = new MyFileSystemTree(myfat, jTreeFileSystem);
            this.myfit = new MyFIT(this.myfat);
        }
           Date a = new Date();
           modififyTime = a.toLocaleString();
           
    }

    private void showIt(){
        JOptionPane.showMessageDialog(null, "亲,我们暂不提供文件操作！") ;
    }
    
    
    private void closeIt(){
        if (editor.getText().equals(file.getMyfileContent())) {
            this.dispose();
        } else {
            JOptionPane.showMessageDialog(null, "是否保存文件", "提示", JOptionPane.INFORMATION_MESSAGE);
            saveIt();
            this.dispose();
        }
//                System.out.println(editor.getText());
    }

//    public void setModififyTime(String mytime){
//        this.modififyTime = mytime;
//    }
//    public String getModififyTime(){
//        return this.modififyTime;
//    }
    /**
    * @param args the command line arguments
    */

    // Variables declaration - do not modify                     
    private javax.swing.JTextArea editor;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItemExit;
    private javax.swing.JMenuItem jMenuItemSave;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JPopupMenu.Separator jSeparator2;
     private javax.swing.JMenuItem jMeauItemShow;
    // End of variables declaration                     
}
